import request from '@/utils/request'

// 部门查询参数
export interface DeptQueryParams {
  deptName?: string
  status?: number
}

// 创建部门请求
export interface CreateDeptRequest {
  parentId?: number
  deptName: string
  deptCode?: string
  leader?: string
  phone?: string
  email?: string
  sort?: number
  status?: number
  remark?: string
}

// 更新部门请求
export interface UpdateDeptRequest {
  parentId?: number
  deptName: string
  deptCode?: string
  leader?: string
  phone?: string
  email?: string
  sort?: number
  status?: number
  remark?: string
}

// 部门API
export const deptApi = {
  // 获取部门树
  getDeptTree: () => {
    return request.get('/departments/tree')
  },

  // 获取所有启用的部门
  getEnabledDepts: () => {
    return request.get('/departments/enabled')
  },

  // 获取所有部门列表
  getDeptList: (params: DeptQueryParams) => {
    return request.get('/departments/list', { params })
  },

  // 根据父部门ID获取子部门
  getDeptsByParentId: (parentId: number) => {
    return request.get(`/departments/children/${parentId}`)
  },

  // 获取部门详情
  getDeptById: (id: number) => {
    return request.get(`/departments/${id}`)
  },

  // 新增部门
  createDept: (data: CreateDeptRequest) => {
    return request.post('/departments', data)
  },

  // 更新部门
  updateDept: (id: number, data: UpdateDeptRequest) => {
    return request.put(`/departments/${id}`, data)
  },

  // 删除部门
  deleteDept: (id: number) => {
    return request.delete(`/departments/${id}`)
  },

  // 批量删除部门
  batchDeleteDept: (ids: number[]) => {
    return request.delete('/departments/batch', { data: { ids } })
  },

  // 启用/禁用部门
  changeStatus: (id: number, status: number) => {
    return request.put(`/departments/${id}/status`, { status })
  },

  // 检查部门编码是否存在
  checkDeptCode: (deptCode: string, excludeId?: number) => {
    return request.get('/departments/check-code', { 
      params: { deptCode, excludeId } 
    })
  },

  // 检查部门是否有子部门
  hasChildren: (id: number) => {
    return request.get(`/departments/${id}/has-children`)
  },

  // 检查部门是否有用户
  hasUsers: (id: number) => {
    return request.get(`/departments/${id}/has-users`)
  }
} 